package cn.stylefeng.guns.sys.core.util;

import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {

    static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 获取当前月的上个月
     *
     * @author liuy
     * @Date 2020-06-03
     */
    public static String getLastMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        //calendar.set（）bug设置一个月时间，要是上个月没有31号的话，系统自动进到这个月
        //calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        calendar.add(Calendar.MONTH, -1);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    /**
     * 获取当前月的上个月
     *
     * @author liuy
     * @Date 2020-06-03
     */
    public static String getLastMonthStr() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    public static String getLastMonthDayStr() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }


    /**
     * 获取当前月
     */
    public static String getCurrentMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        String month = format.format(date);
        return month;
    }

    /**
     * 获取当前月的下个月
     *
     * @author liuy
     * @Date 2020-06-03
     */
    public static String getNextMonthStr() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    public static String getSixMonthStr() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 5);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    public static String getYeatStr() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 11);
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    /**
     * 获取当前月的第一天
     *
     * @return
     */
    public static String getFirstDayOfCurrentMonth() {
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH, 1);
        String first = format.format(c.getTime());
        return first;
    }

    /**
     * @param mon  为增加的月数
     * @param date 指定日期
     * @return
     */
    public static String addMonth(String date, int mon) {
        Calendar c = Calendar.getInstance();

        c.setTime(new Date());
        c.add(Calendar.MONTH, mon);
        Date m = c.getTime();
        String first = format.format(m);
        return first;
    }

    /**
     * @param hour 为增加的小时
     * @param date 指定日期
     * @return
     */
    public static Date addHour(String date, int hour) {
        Calendar c = Calendar.getInstance();

        c.setTime(new Date());
        c.add(Calendar.HOUR, hour);//24小时制
        Date m = c.getTime();
        return m;
    }

    /**
     * @param year 为增加的年数
     * @param date 指定日期
     * @return
     */
    public static String addYear(String date, int year) {
        Calendar c = Calendar.getInstance();

        c.setTime(new Date());
        c.add(Calendar.YEAR, year);
        Date m = c.getTime();
        String first = format.format(m);
        return first;
    }

    /**
     * 获取某月的第一天
     *
     * @return
     */
    public static String getFirstDayOfMonthByParam(int before) {
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0 - before);
        c.set(Calendar.DAY_OF_MONTH, 1);
        String first = format.format(c.getTime());
        return first;
    }

    public static void main(String[] args) {
        getFirstDayOfMonthByParam(1);
    }

    /**
     * 当前月起始时间
     *
     * @return
     */
    public static String getStartTimeOfCurrentMonth() {
        return getFirstDayOfCurrentMonth() + " 00:00:00";
    }

    /**
     * 获取当前月的最后一天
     *
     * @return
     */
    public static String getLastDayOfCurrentMonth() {
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = format.format(ca.getTime());
        return last;
    }

    /**
     * 当前月结束时间
     *
     * @return
     */
    public static String getEndTimeOfCurrentMonth() {
        return getLastDayOfCurrentMonth() + " 23:59:59";
    }

    /**
     * 获取某月的第一天
     */
    public static String getFisrtDayOfMonth(String date) {

        int year = Integer.valueOf(date.substring(0, 4));
        int month = Integer.valueOf(date.substring(5, 7));


        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    /**
     * 获取某月的起始时间
     */
    public static String getStartTimeOfMonth(String date) {

        return getFisrtDayOfMonth(date) + " 00:00:00";
    }


    /**
     * 获取某月的最后一天
     */
    public static String getLastDayByMonth(String date) {
        int year = Integer.valueOf(date.substring(0, 4));
        int month = Integer.valueOf(date.substring(5, 7));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    public static String getLastDayByMonths(String date) {
        int year = Integer.valueOf(date.substring(0, 4));
        int month = Integer.valueOf(date.substring(5, 7));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 2);
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }


    /**
     * 获取某月的结束时间
     */
    public static String getEndTimeOfMonth(String date) {

        return getLastDayByMonth(date) + " 23:59:59";
    }

    /**
     * 获取某个月的天数,传入年月格式202010,返回具体的天数
     *
     * @author liuy
     * @Date 2020-10-13
     */
    public static int getDaysOfMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        Date date1 = sdf.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date1);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }


    /****
     * 传入具体日期 ，返回具体日期增加一个月。
     * @author liuy
     *@Date 2020-06-03
     * @throws ParseException
     */
    public static String subMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, 1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /****
     * 传入具体日期 ，返回具体日期增加两个月。
     * @author liuy
     *@Date 2020-06-03
     * @throws ParseException
     */
    public static String subTwoMonth(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, 2);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * @param day  为增加的天数
     * @param date 指定日期
     * @return 返回类型 String
     * @throws ParseException
     */
    public static String addDate(String date, long day) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date dates = dateFormat.parse(date);
        //得到指定日期的毫秒数
        long time = dates.getTime();
        //要加上的天数转换成毫秒数
        day = day * 24 * 60 * 60 * 1000;
        //相加得到新的毫秒数
        time += day;
        //将毫秒数转换成日期
        return dateFormat.format(new Date(time));
    }


    /**
     * @param day 当前天
     * @return 返回类型 java.lang.String
     * @throws
     * @Title: getYearstoday
     * @Description: 获取某一天的前一天（昨天）
     * @author gejiang
     */
    public static String getYearstoday(Date day) {
        Date backupTime = DateUtils.addDays(day, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(backupTime);
    }


    /**
     * 获取前两天日期
     *
     * @param day
     * @return
     */
    public static List<String> getTwoDaysAgoByDate(Date day) {
        List<String> dayList = new ArrayList<>();
        Date backupTime = DateUtils.addDays(day, -1);
        Date backupTime2 = DateUtils.addDays(day, -2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        dayList.add(sdf.format(backupTime2));
        dayList.add(sdf.format(backupTime));
        return dayList;
    }

    public static String getYearstoday(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Date backupTime = DateUtils.addDays(dt, -1);
        return sdf.format(backupTime);
    }

    public static String getDay(String date, int next) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Date backupTime = DateUtils.addDays(dt, next);
        return sdf.format(backupTime);
    }

    /**
     * 转换时间格式
     *
     * @param time 参数
     * @return 转换之后的时间格式
     */
    public static Date getTimeToDate(String time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }


    /**
     * @param startTime, endTime
     * @return 返回类型 java.util.List<java.lang.String>
     * @throws
     * @Title: getBetweenDate
     * @Description: 获取两个字符串日期之间的所有字符串日期
     * @author gejiang
     */
    public static List<String> getBetweenDate(String startTime, String endTime) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            // 转化成日期类型
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);

            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                //System.out.println(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.DATE, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取默认的时间字符串 ，形如2020-09-01 11:11:11
     *
     * @return
     * @author gyz
     */
    public static String getNow() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(new Date());
        return now;
    }

    public static String subtractTime(Date date, int amount) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(date.getTime() + amount);
            Date time = sdf.parse(strTime);
            String now = sdf.format(time);
            return now;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static String getDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = sdf.format(date);
        return now;
    }


    /**
     * 计算end-start的间隔天数
     *
     * @param start
     * @param end
     * @return
     * @throws ParseException
     */
    public static long getDaysNum(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = sdf.parse(start);
        Date d2 = sdf.parse(end);
        long l = (d2.getTime() - d1.getTime()) / (3600 * 24 * 1000);
        return l;
    }

    /**
     * 获取今天日期 ，形如2020-09-01
     *
     * @return
     * @author gyz
     */
    public static String getNowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(new Date());
        return now;
    }

    /**
     * 获取今天日期 ，形如2020-09-01
     *
     * @return
     * @author gyz
     */
    public static String getStrDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String now = sdf.format(date);
        return now;
    }

    /**
     * 获取本月的第一天
     *
     * @return
     */
    public static String getMonthFirstDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String day = sdf.format(calendar.getTime());
        return day;
    }

    /**
     * 获取某个月前的第一天
     *
     * @return
     */
    public static String getMonthFirstDayByAgo(int ago) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 0 - ago);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String day = sdf.format(calendar.getTime());
        return day;
    }

    /**
     * 获取某个月的最后一天
     *
     * @return
     */
    public static String getMonthLastDayByAgo(int ago) {
        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.MONTH, 1 - ago);
        calendar2.set(Calendar.DAY_OF_MONTH, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay = sdf.format(calendar2.getTime());
        return lastDay;

    }

    /**
     * 昨天4点
     *
     * @return
     */
    public static String getYesterDay4Oclock() {
        Date backupTime = DateUtils.addDays(new Date(), -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
        String date = sdf.format(backupTime) + "04:00:00";
        return date;
    }

    /**
     * 昨天0点
     *
     * @return
     */
    public static String getYesterDayOclock() {
        Date backupTime = DateUtils.addDays(new Date(), -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(backupTime) + "000000";
        return date;
    }

    public static String getYesterDay() {
        Date backupTime = DateUtils.addDays(new Date(), -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(backupTime);
        return date;
    }

    public static String getYesterDayDate() {
        Date backupTime = DateUtils.addDays(new Date(), -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(backupTime);
        return date;
    }

    /**
     * 昨天23点
     *
     * @return
     */
    public static String getYesterDay23Oclock() {
        Date backupTime = DateUtils.addDays(new Date(), -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(backupTime) + "235959";
        return date;
    }

    /**
     * 获取昨天的时间戳：yyyyMMddHHmmss
     */
    public static String getYesterDayTime() {
        Date backupTime = DateUtils.addDays(new Date(), -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(backupTime);
        return date;
    }


    /**
     * 今天4点
     *
     * @return
     */
    public static String getToday4Oclock() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date()) + " 04:00:00";
        return date;
    }


    /**
     * 明天4点
     *
     * @return
     */
    public static String getNextDay4Oclock() {
        Date backupTime = DateUtils.addDays(new Date(), 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(backupTime) + " 04:00:00";
        return date;
    }

    public static String get4OclockByDay(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = null;
        try {
            d = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Date backupTime = DateUtils.addDays(d, 1);
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf2.format(backupTime) + " 04:00:00";

        return dateStr;
    }

    public static String get4OclocksBeforeDays(String day, int days) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(day);
        Date backupTime = DateUtils.addDays(dt, 0 - days);
        String date = sdf.format(backupTime) + " 04:00:00";

        return date;
    }

    /**
     * 日期字符串yyyy-MM-dd 转Date
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date getDateByStr(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d = sdf.parse(date);
        return d;
    }

    /**
     * 获取当前时间戳：yyyyMMddHHmmss
     *
     * @return
     * @throws ParseException
     */
    public static String getStampNow(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String str = sdf.format(new Date());
        return str;
    }

    /****
     * 传入具体日期 ，返回具体日期减少一个月。
     * @author jiangge
     *@Date 2020-12-11
     * @throws ParseException
     */
    public static String lastMonthByDate(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, -1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 根据时间戳转为字符串日期
     *
     * @param stamp
     * @return
     */
    public static String getDate(Long stamp) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(new Date(stamp));


        return format;
    }


    /**
     * 根据当前日期获得上周的日期区间（上周周一和周日日期）
     *
     * @return
     */
    public static List<String> getLastWeekInterval(String day) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(day);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(dt);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(dt);
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        System.out.println(sdf.format(calendar1.getTime()));// last Monday
        String lastBeginDate = sdf.format(calendar1.getTime());
        System.out.println(sdf.format(calendar2.getTime()));// last Sunday
        String lastEndDate = sdf.format(calendar2.getTime());
        List<String> betweenDate = getBetweenDate(lastBeginDate, lastEndDate);
        return betweenDate;
    }

    /**
     * 获取今日是本周的第几天
     */
    public static int getToDayLastWeek() {

        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);

        System.out.println("今天时本周的第" + day + "天");

        return day;

    }

    /**
     * 根据当前日期获得所在周的日期区间（周一和周日日期）
     */
    public static List<String> getThisWeekInterval(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        System.out.println("所在周星期日的日期：" + imptimeEnd);
        List<String> betweenDate = getBetweenDate(imptimeBegin, imptimeEnd);
        return betweenDate;
    }

    /**
     * 根据当前日期获得所在周的日期区间（周一和周日日期）
     */
    public static List<String> getThisWeekIntervalToThisDay(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        System.out.println("所在周星期日的日期：" + imptimeEnd);
        List<String> betweenDate = getBetweenDate(imptimeBegin, date);
        return betweenDate;
    }


    /**
     * 上周本日
     *
     * @param date
     * @return
     * @throws ParseException
     */
    public static String getToDayLastWeek(String date) throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int day1 = cal.get(Calendar.DAY_OF_WEEK);

        int day = day1 - 1;

        if (day == 0) {
            day = 7;
        }

        System.out.println("date是本周的第" + day + "天");


        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(dt);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(dt);
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        //System.out.println(sdf.format(calendar1.getTime()));// last Monday
        String lastBeginDate = sdf.format(calendar1.getTime());
        // System.out.println(sdf.format(calendar2.getTime()));// last Sunday
        String lastEndDate = sdf.format(calendar2.getTime());


        List<String> betweenDate = getBetweenDate(lastBeginDate, lastEndDate);

        return betweenDate.get(day - 1);
    }


    public List<String> getMonthDay(String date) {
        List<String> list = getMonthFullDay(date);
        List<String> listDay = new ArrayList<>();
        for (String date1 : list) {
            listDay.add(date1.substring(8, 10));
        }
        return listDay;
    }

    /**
     * 获取本月所有日期
     *
     * @param date
     * @return
     */
    public static List<String> getMonthFullDay(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<String>();
        int year = Integer.parseInt(date.substring(0, 4));
        int month = Integer.parseInt(date.substring(5, 7));
        int day = 1;// 所有月份从1号开始
        Calendar cal = Calendar.getInstance();// 获得当前日期对象
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);// 1月从0开始
        cal.set(Calendar.DAY_OF_MONTH, day);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 0; j <= (count - 1); ) {
            if (sdf.format(cal.getTime()).equals(getLastDay(year, month)))
                break;
            cal.add(Calendar.DAY_OF_MONTH, j == 0 ? +0 : +1);
            j++;
            fullDayList.add(sdf.format(cal.getTime()));
        }
        return fullDayList;
    }

    public static String getLastDay(int year, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month);
        cal.set(Calendar.DAY_OF_MONTH, 0);
        return sdf.format(cal.getTime());
    }


    /**
     * 获取本月的第一天
     *
     * @return
     */
    public static String getMonthFirstDayByDay(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String day = sdf.format(calendar.getTime());
        return day;
    }


    /**
     * 获取失效时间(4小时)
     */
    public static String afterDate(Date createDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = format.format(createDate);
        format.setTimeZone(TimeZone.getTimeZone("GMT+0"));
        Date date = format.parse(dateStr);
        format.setTimeZone(TimeZone.getTimeZone("GMT+4"));
        dateStr = format.format(date);
        return dateStr;
    }

    /**
     * 计算当前时间与到期时间的时间差
     */
    public static Long difference(Date day, String afterDate) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = df.parse(afterDate);
        long diff = d1.getTime() - day.getTime();
        return diff;
    }

    public static void Test() throws ParseException {
//        getTimeInterval(new Date());
//        所在周星期一的日期：2021-05-10
//        所在周星期日的日期：2021-05-16

        //1.昨日
        String yearstoday = getYearstoday("2021-03-31");
        System.out.println("昨日：" + yearstoday);

        //2.上周本日
        String toDayLastWeek = getToDayLastWeek("2021-03-31");
        System.out.println("上周的今天：" + toDayLastWeek);

        //本周累计
        List<String> lastTimeInterval1 = getThisWeekIntervalToThisDay("2021-03-31");
        System.out.println("本周累计：" + lastTimeInterval1);

        //上周累计
        List<String> lastTimeInterval2 = getLastWeekInterval("2021-03-31");
        System.out.println("上周累计：" + lastTimeInterval2);

        //本月累计
        //List<String> monthFullDay = getMonthFullDay("2021-03-31");
        List<String> betweenDate = getBetweenDate(getMonthFirstDayByDay("2021-02-20"), "2021-02-20");
        System.out.println("本月累计：" + betweenDate);

        //上月累计
        List<String> monthFullDay2 = getMonthFullDay(lastMonthByDate("2021-03-31"));
        System.out.println("上月累计：" + monthFullDay2);

        String oclocksBeforeDays = get4OclocksBeforeDays("2021-05-12", 2);
        System.out.println("8天前的日期为：" + oclocksBeforeDays);


        String day = getDay("2021-05-12", -3);
        System.out.println(day);

    }

    //获取时间差方法
    public static String getTime(Date currentTime, Date firstTime) {
        long diff = currentTime.getTime() - firstTime.getTime();//这样得到的差值是微秒级别
        Calendar currentTimes = dataToCalendar(currentTime);//当前系统时间转Calendar类型
        Calendar firstTimes = dataToCalendar(firstTime);//查询的数据时间转Calendar类型
        int year = currentTimes.get(Calendar.YEAR) - firstTimes.get(Calendar.YEAR);//获取年
        int month = currentTimes.get(Calendar.MONTH) - firstTimes.get(Calendar.MONTH);//月
        int day = currentTimes.get(Calendar.DAY_OF_MONTH) - firstTimes.get(Calendar.DAY_OF_MONTH);
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1);
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
        }
        if (month < 0) {
            month = (month + 12) % 12;//获取月
            year--;
        }
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); //获取时
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);  //获取分钟
        long s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);//获取秒
        String CountTime = "" + hours + "小时" + minutes + "分" + s + "秒";
        return CountTime;
    }

    //Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    // 截取非数字
    public static String splitNotNumber(String content) {
        Pattern pattern = Pattern.compile("\\D+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
    /**
     * 过滤非数字
     * @param str
     * @return
     */
    public static String getNumeric(String str) {
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }

    //获取某个日期月份的第一天
    public static String getMonthFirstDay(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt = sdf.parse(date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String day = sdf.format(calendar.getTime());
        return day;
    }

    //获取某个日期月份的最后一天
    public static String getMonthLastDay(String date) {
        int year = Integer.valueOf(date.substring(0, 4));
        int month = Integer.valueOf(date.substring(4, 6));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 1);
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    //获取某个日期上个月份的最后一天
    public static String getMonthLastDays(String date) {
        int year = Integer.valueOf(date.substring(0, 4));
        int month = Integer.valueOf(date.substring(4, 6));
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, month - 2);
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    //获取两个日期差多少天
    public static long getDistanceDays(String str1, String str2) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyyMMdd");
        Date one;
        Date two;
        long days = 0;
        one = df.parse(str1);
        two = df.parse(str2);
        long time1 = one.getTime();
        long time2 = two.getTime();
        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        days = diff / (1000 * 60 * 60 * 24);
        return days;
    }

    /**
     * 字符串去掉最后一位逗号
     * @param s
     * @return
     */
    public static String comma(String s) {
        if (s.charAt(s.length() - 1) == ',') {
            return s.substring(0, s.length() - 1);
        } else {
            return s;
        }
    }

    /**
     * 获取指定日期的时间戳
     */
    public  static long  getTimestamp(String dates) throws ParseException {

        DateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date = df.parse(dates);
        Calendar cal = Calendar.getInstance(); cal.setTime(date);
        long timestamp =cal.getTimeInMillis();
        //System.out.println(timestamp);
        //System.out.println(timestamp/1000);
        return  timestamp/1000;
    }
    /**
     * 获取前天的日期
     */
    public static String getLastYearstoday(Date day) {
        Date backupTime = DateUtils.addDays(day, -2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(backupTime);
    }

    public static int appearNumber(String srcText,String findText){
        int count=0;
        Pattern p=Pattern.compile(findText);
        Matcher m=p.matcher(srcText);
        while (m.find()){
            count++;
        }
       return  count;
    }

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }
}
